/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arvore.tabelas;

import camada.bancodedados.daoimplemente.mysql.MysqlDAOImpl;
import eloma.classe.mysql.Campos;
import java.util.ArrayList;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 *
 * @author KLINGER
 */
public class TreeModel extends DefaultTreeModel {

    MysqlDAOImpl daoMy = new MysqlDAOImpl();
    private DefaultMutableTreeNode root;
    private String rootName = "";

    public TreeModel(TreeNode root) {
        super(root);
        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) root;
        rootName = (String) parentNode.getUserObject();
    }

    @Override
    public Object getChild(Object parent, int index) {

        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parent;
        String parentName = (String) parentNode.getUserObject();

        if (parentName.equals(rootName)) {
            return super.getChild(parent, index);
        } else {
            DefaultMutableTreeNode nod = null;

            List<Campos> lista = new ArrayList<Campos>();

            try {

                lista = daoMy.getCamposTabela(parent.toString());

                Campos c = lista.get(index);

                nod = new DefaultMutableTreeNode(c.getField());

            } catch (Exception ex) {
            }

            return nod;

        }
    }

}
